import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    int m = 0, n = 0;
    int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};
    Queue<int[]> q = new LinkedList<>();

    public void solve(char[][] board) {
        m = board.length;
        n = board[0].length;

        //处理边界情况
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
                    if (board[i][j] == 'O') {
                        bfs(board, i, j, '*');
                    }
                }
            }
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (board[i][j] == 'O') {
                    bfs(board, i, j, 'X');
                }
            }
        }

        //将边界情况改回来
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == '*') {
                    board[i][j] = 'O';
                }
            }
        }
    }

    public void bfs(char[][] board, int i, int j, char change) {
        q.offer(new int[]{i, j});
        board[i][j] = change;
        while (!q.isEmpty()) {
            int[] arr = q.poll();
            int x = arr[0];
            int y = arr[1];
            board[x][y] = change;
            for (int k = 0; k < 4; k++) {
                int xx = x + dx[k];
                int yy = y + dy[k];
                if (xx >= 0 && yy >= 0 && xx < m && yy < n && board[xx][yy] == 'O') {
                    q.offer(new int[]{xx, yy});
                    board[xx][yy] = change;
                }
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        char[][] arr = new char[][]{{'O', 'O'}, {'O', 'O'}};
        solution.solve(arr);
        System.out.println(Arrays.toString(arr));
    }
}
